package jubao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import utils.TimeUtil;


/**
 * Created by Administrator on 2016/12/22.
 */
public class JuBaoDeal extends UIHandler.BaseActivity {

    Button btn_jubaoviewall;
    EditText et_time_jubao;
    EditText et_person_jubao;
    EditText et_tel_jubao;
    EditText et_addr_jubao;
    EditText et_person_luru;
    EditText et_time_luru;
    EditText et_content_luru;

    Button btn_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jubaodeal);
        initview();
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == btn_jubaoviewall)
            {
                Intent intent = new Intent(JuBaoDeal.this, JuBaoView.class);
                JuBaoDeal.this.startActivity(intent);
            }
            else if (view == btn_add)
            {
                if (checked())
                    sendRequest();
            }
        }
    };

    // 输入是否合法
    private boolean checked() {
        if (et_time_jubao.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"举报时间不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!et_time_jubao.getText().toString().matches("[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]"))
        {
            Toast.makeText(MyApplication.share(),"举报时间格式不正确",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (et_person_jubao.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"举报人不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (et_tel_jubao.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"举报电话不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!et_tel_jubao.getText().toString().matches("[0-9]{11}"))
        {
            Toast.makeText(MyApplication.share(),"举报电话格式不正确",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (et_addr_jubao.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"举报地址不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }


        if (et_person_luru.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"录入人不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (et_time_luru.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"录入时间不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!et_time_luru.getText().toString().matches("[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]"))
        {
            Toast.makeText(MyApplication.share(),"录入时间格式不正确",Toast.LENGTH_SHORT).show();
            return false;
        }

        if (et_content_luru.getText().toString().equals(""))
        {
            Toast.makeText(MyApplication.share(),"录入内容不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    // 发送添加请求
    private void sendRequest() {
        String IP = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url ="http://"+IP+"/GrassrootsOpinionAction";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (response.equals("\"success\""))
                        {
                            Toast.makeText(MyApplication.share(),"添加成功",Toast.LENGTH_SHORT).show();
                            afterSuccess();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyApplication.share(),"添加失败",Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("grassrootsOpinion.crowdTime",et_time_jubao.getText().toString());
                map.put("grassrootsOpinion.crowdName",et_person_jubao.getText().toString());
                map.put("grassrootsOpinion.crowdTel",et_tel_jubao.getText().toString());
                map.put("grassrootsOpinion.crowdArea",et_addr_jubao.getText().toString());
                map.put("grassrootsOpinion.InputName",et_person_luru.getText().toString());
                map.put("grassrootsOpinion.InputTime",et_time_luru.getText().toString());
                map.put("grassrootsOpinion.crowdContext",et_content_luru.getText().toString());
                return  map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    private void afterSuccess() {
        et_addr_jubao.setText("");
        et_content_luru.setText("");
        et_person_jubao.setText("");
        et_tel_jubao.setText("");
        et_person_luru.setText("");
        et_time_jubao.setText("");
        et_time_luru.setText("");
    }

    private void initview() {
        btn_jubaoviewall = (Button) findViewById(R.id.id_btn_viewall);
        btn_jubaoviewall.setOnClickListener(onClickListener);

        et_time_jubao = (EditText) findViewById(R.id.id_time_jubao);
        et_time_jubao.setOnClickListener(onClickListener);

        et_person_jubao = (EditText) findViewById(R.id.id_et_people_jubao);
        et_person_jubao.setOnClickListener(onClickListener);
        et_tel_jubao = (EditText) findViewById(R.id.id_et_tel_jubao);
        et_tel_jubao.setOnClickListener(onClickListener);
        et_addr_jubao = (EditText) findViewById(R.id.id_et_address_jubao);
        et_addr_jubao.setOnClickListener(onClickListener);
        et_person_luru = (EditText) findViewById(R.id.id_et_record_jubao);
        et_person_luru.setOnClickListener(onClickListener);
        et_time_luru = (EditText) findViewById(R.id.id_et_time_luru);
        et_time_luru.setOnClickListener(onClickListener);
        et_content_luru = (EditText) findViewById(R.id.id_et_content_jubao);
        et_content_luru.setOnClickListener(onClickListener);

        btn_add = (Button) findViewById(R.id.id_btn_addJubao);
        btn_add.setOnClickListener(onClickListener);

        String time = TimeUtil.currentTimeSP2();
        et_time_luru.setText(time);
        et_time_jubao.setText(time);
    }
}
